import numpy as np
import csv


def load_data(file_path):
    data = []
    with open(file_path, 'r') as f:
        reader = csv.reader(f)
        next(reader)  # Skip header row
        for row in reader:
            math = int(row[2])
            english = int(row[3])
            python = int(row[4])
            data.append([math, english, python])
    return np.array(data)


def calculate_statistics(data):
    math = data[:, 0]
    english = data[:, 1]
    python = data[:, 2]

    math_mean = np.round(np.mean(math), 1)
    english_mean = np.round(np.mean(english), 1)
    python_mean = np.round(np.mean(python), 1)

    math_median = np.round(np.median(math), 1)
    english_median = np.round(np.median(english), 1)
    python_median = np.round(np.median(python), 1)

    math_var = np.round(np.var(math, ddof=0), 1)
    english_var = np.round(np.var(english, ddof=0), 1)
    python_var = np.round(np.var(python, ddof=0), 1)

    math_std = np.round(np.std(math, ddof=0), 1)
    english_std = np.round(np.std(english, ddof=0), 1)
    python_std = np.round(np.std(python, ddof=0), 1)

    return {
        'means': [math_mean, english_mean, python_mean],
        'medians': [math_median, english_median, python_median],
        'variances': [math_var, english_var, python_var],
        'stds': [math_std, english_std, python_std]
    }


def print_results(stats):
    subjects = ['Math', 'English', 'Python']
    for i, subject in enumerate(subjects):
        print(f"{subject}:")
        print(f"    Average: {stats['means'][i]:.1f}")
        print(f"    Median: {stats['medians'][i]:.1f}")
        print(f"    Variance: {stats['variances'][i]:.1f}")
        print(f"    Standard Deviation: {stats['stds'][i]:.1f}")
    print()

    max_mean = max(stats['means'])
    best_index = stats['means'].index(max_mean)
    best_subject = subjects[best_index]

    min_std = min(stats['stds'])
    consistent_index = stats['stds'].index(min_std)
    consistent_subject = subjects[consistent_index]
    std_value = stats['stds'][consistent_index]

    print(f"Best Subject: {best_subject}")
    print(f"Most Consistent Subject (Lowest Std): {consistent_subject} ({std_value:.1f})")


def main():
    data = load_data('scores.csv')
    stats = calculate_statistics(data)
    print_results(stats)


if __name__ == "__main__":
    main()